import { baseApiUrl, path } from "../../utils/config";
import { orderInfo, orderCancel, orderDelivery } from '../../utils/api';
import Toast from '@vant/weapp/toast/toast';
const app = getApp();
Page({
  data: {
    baseImageUrl: baseApiUrl,
    id: null,

    username: "",
    phone: "",
    address: "",

    cover: "",
    name: "",
    price: "",
    total: "",
    freight: "",
    order_no: "",
    created_at: "",
    pay_date: "",
    complete_date: "",
    status: null,

    rato: 1
  },
  async onLoad(options) {
    let { id } = options;
    this.setData({ id });
    this.getDeta();
  },
  async getDeta() {
    let { id } = this.data;
    let { order, integral_rato } = await orderInfo({ id });
    let { address, order_no, created_at, freight, status, product, pay_date, complete_date, number } = order;

    let username = address.name;
    let phone = address.phone;
    address = `${address.province}${address.city}${address.area}${address.address}`;

    let cover = product?.thumbnail;
    let name = product?.name;
    let synopsis = product?.synopsis;
    let price = product?.price;

    this.setData({
      username, phone, address, order_no, created_at,
      pay_date, complete_date, freight, status, cover,
      name, synopsis, price, count: number, rato: integral_rato
    });
    this.diff_price();
  },
  diff_price() {
    let { price, count, rato } = this.data;
    let total = (Number(price) * Number(count)).toFixed(4);
    let total_integral = (total / rato).toFixed(0);
    this.setData({ total, total_integral });
  },
  async cancel() {
    let { id } = this.data;
    await orderCancel({ id });
    Toast.success('操作成功');
    this.getDeta();
  },
  async delivery() {
    let { id } = this.data;
    await orderDelivery({ id });
    Toast.success('操作成功');
    this.onLoad();
  },
  router(e) {
    let { url } = e.currentTarget.dataset;
    wx.navigateTo({ url });
  },
  onShareAppMessage() {
    let poster = app.globalData.poster;
    return {
      path,
      imageUrl: poster
    }
  },
  onShareTimeline() { }
})